const nunjucks = require("nunjucks");

function createEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let confOpts = {
        autoescape: opts.autoescape || true,
        throwOnUndefined: opts.throwOnUndefined || false,
        trimBlocks: opts.trimBlocks || true,
        lstripBlocks: opts.lstripBlocks || true,
        watch: opts.watch || true,
        noCache: opts.noCache || true
    }
    let env = nunjucks.configure(path, confOpts);
    return env;
}



module.exports = async (ctx, next) => {
    let env = createEnv();

    ctx.render = function (view, model) {
        ctx.body = nunjucks.render(view, model);
    }
    await next();
}

